/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.ugame.model.bl;

import java.util.List;
import java.util.Locale;
import vn.ugame.model.dal.StoreDA;
import vn.ugame.model.entity.Comment;
import vn.ugame.model.entity.Image;
import vn.ugame.model.entity.ImageType;
import vn.ugame.model.entity.Item;

/**
 *
 * @author Namtp
 */
public class StoreService {

    private Locale locale;
    private StoreDA storeDA;

    public StoreService(Locale locale) {
        this.locale = locale;
        storeDA = new StoreDA(locale);
    }

    public Locale getLocale() {
        return this.locale;
    }

    public FindResult<Item> findItems(String moduleId, String keyword, int page) {
        int pageSize = 4;

        FindResult<Item> result = new FindResult<Item>();

        //StoreDA storeDA = new StoreDA(getLocale());  

        result.setPage(page);
        result.setKeyword(keyword);

        List<Item> items = storeDA.findItems(moduleId, keyword, page, pageSize);
        result.setResult(items);

        int count = storeDA.count(moduleId, keyword);
        result.setCount(count);

        int pageCount = 1;
        if (!items.isEmpty()) {
            pageCount = (int) Math.ceil(count / pageSize);
        }
        result.setPageCount(pageCount);

        return result;
    }

    public Item findItemById(String id) {
        return storeDA.findItemById(id);
    }
    
    public Item findItem(String id, String name) {
        return storeDA.findItem(id, name);
    }
    
    public List<Item> findLocalItems(String id){
        return storeDA.findLocalItems(id);
    }
    
    public List<Comment> findComments(String itemId){
        return storeDA.findComments(itemId);
    }

    public Image findImage(String itemId, int type) {
        return storeDA.findImages(type, itemId, 1).get(0);
    }

    public List<Image> getScreenShots(String itemId) {
        return storeDA.findImages(ImageType.SCREEN_SHOT, itemId, -1);
    }
}
